package org.jeecg.modules.gift.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;

import org.jeecg.modules.gift.entity.GiftBusinessDistrict;
import org.jeecg.modules.gift.entity.GiftMerchant;
import org.jeecg.modules.gift.service.IGiftBusinessDistrictService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.gift.service.IGiftMerchantService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * @Description: 商场
 * @Author: jeecg-boot
 * @Date: 2024-07-06
 * @Version: V1.0
 */
@Api(tags = "商场")
@RestController
@RequestMapping("/gift/giftBusinessDistrict")
@Slf4j
public class GiftBusinessDistrictController extends JeecgController<GiftBusinessDistrict, IGiftBusinessDistrictService> {
    @Autowired
    private IGiftBusinessDistrictService giftBusinessDistrictService;
    @Autowired
    private IGiftMerchantService giftMerchantService;

    /**
     * 分页列表查询
     *
     * @param giftBusinessDistrict
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "商场-分页列表查询")
    @ApiOperation(value = "商场-分页列表查询", notes = "商场-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<GiftBusinessDistrict>> queryPageList(GiftBusinessDistrict giftBusinessDistrict,
                                                             @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                             HttpServletRequest req) {
        QueryWrapper<GiftBusinessDistrict> queryWrapper = QueryGenerator.initQueryWrapper(giftBusinessDistrict, req.getParameterMap());
        Page<GiftBusinessDistrict> page = new Page<GiftBusinessDistrict>(pageNo, pageSize);
        IPage<GiftBusinessDistrict> pageList = giftBusinessDistrictService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param giftBusinessDistrict
     * @return
     */
    @AutoLog(value = "商场-添加")
    @ApiOperation(value = "商场-添加", notes = "商场-添加")
    @RequiresPermissions("gift:gift_business_district:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody GiftBusinessDistrict giftBusinessDistrict) {

        LambdaQueryWrapper<GiftBusinessDistrict> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GiftBusinessDistrict::getAdmin, giftBusinessDistrict.getAdmin());
        if (giftBusinessDistrictService.count(queryWrapper) > 0)
            return Result.error("管理员" + giftBusinessDistrict.getAdmin() + "已绑定其他商场");

        giftBusinessDistrictService.save(giftBusinessDistrict);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param giftBusinessDistrict
     * @return
     */
    @AutoLog(value = "商场-编辑")
    @ApiOperation(value = "商场-编辑", notes = "商场-编辑")
    @RequiresPermissions("gift:gift_business_district:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody GiftBusinessDistrict giftBusinessDistrict) {

        LambdaQueryWrapper<GiftBusinessDistrict> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GiftBusinessDistrict::getAdmin, giftBusinessDistrict.getAdmin());
        queryWrapper.ne(GiftBusinessDistrict::getId, giftBusinessDistrict.getId());
        if (giftBusinessDistrictService.count(queryWrapper) > 0)
            return Result.error("管理员" + giftBusinessDistrict.getAdmin() + "已绑定其他商场");

        giftBusinessDistrictService.updateById(giftBusinessDistrict);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "商场-通过id删除")
    @ApiOperation(value = "商场-通过id删除", notes = "商场-通过id删除")
    @RequiresPermissions("gift:gift_business_district:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        LambdaQueryWrapper<GiftMerchant> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GiftMerchant::getMail, id);
        if (giftMerchantService.count(queryWrapper) > 0)
            return Result.error("商场下存在店铺，无法删除!");

        giftBusinessDistrictService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "商场-批量删除")
    @ApiOperation(value = "商场-批量删除", notes = "商场-批量删除")
    @RequiresPermissions("gift:gift_business_district:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        return Result.error("不支持批量删除");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "商场-通过id查询")
    @ApiOperation(value = "商场-通过id查询", notes = "商场-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<GiftBusinessDistrict> queryById(@RequestParam(name = "id", required = true) String id) {
        GiftBusinessDistrict giftBusinessDistrict = giftBusinessDistrictService.getById(id);
        if (giftBusinessDistrict == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(giftBusinessDistrict);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param giftBusinessDistrict
     */
    @RequiresPermissions("gift:gift_business_district:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, GiftBusinessDistrict giftBusinessDistrict) {
        return super.exportXls(request, giftBusinessDistrict, GiftBusinessDistrict.class, "商场");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("gift:gift_business_district:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, GiftBusinessDistrict.class);
    }

}
